﻿Public Class fRetenciones
    Dim daReten As lbGlobalcom.dsContabilidadTableAdapters.CON_RETENCIONTableAdapter
    Dim daClave As lbGlobalcom.dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter

    Private Sub fRetenciones_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Me.DsContabilidad.CON_RETENCION.GetChanges IsNot Nothing Then
                CON_RETENCIONBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    CON_RETENCIONBindingNavigatorSaveItem.PerformClick()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub fRetenciones_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daReten = New lbGlobalcom.dsContabilidadTableAdapters.CON_RETENCIONTableAdapter
            daClave = New lbGlobalcom.dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub btnCargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargar.Click
        Try
            If Me.DsContabilidad.CON_RETENCION.GetChanges IsNot Nothing Then
                CON_RETENCIONBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    CON_RETENCIONBindingNavigatorSaveItem.PerformClick()
                End If
            End If
            daReten.Fill(DsContabilidad.CON_RETENCION)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub CON_RETENCIONBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CON_RETENCIONBindingNavigatorSaveItem.Click
        Try
            Validate()
            CON_RETENCIONBindingSource.EndEdit()
            daReten.Update(DsContabilidad.CON_RETENCION)
            MessageBox.Show("Se salvarón registros con exito", "Salvar Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub CON_RETENCIONDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles CON_RETENCIONDataGridView.CellBeginEdit
        Try
            If e.ColumnIndex = 1 AndAlso CON_RETENCIONDataGridView.Rows(e.RowIndex).Cells(0).Value Is DBNull.Value Then
                Dim dt As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASDataTable
                dt = daClave.GetDataByIdClavePrimaria("CON_RETENCION")
                If dt.Count Then
                    Dim dr As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASRow
                    dr = dt.Rows(0)
                    Dim Cons As lbGlobalcom.ConsecTablas = New lbGlobalcom.ConsecTablas(dr("TABLA"), dr("CAMPO"), dr("LONGITUD"))
                    CON_RETENCIONDataGridView.Rows(e.RowIndex).Cells(0).Value = Cons.NumeroConsec
                    Cons = Nothing
                    CON_RETENCIONBindingSource.EndEdit()
                    daReten.Update(DsContabilidad.CON_RETENCION)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub CON_RETENCIONDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles CON_RETENCIONDataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub

    Private Sub CON_RETENCIONDataGridView_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles CON_RETENCIONDataGridView.UserDeletedRow
        Try
            daReten.Update(DsContabilidad.CON_RETENCION)
            MessageBox.Show("Se elimino registro con exito", "Elimina Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub CON_RETENCIONDataGridView_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles CON_RETENCIONDataGridView.UserDeletingRow
        If MessageBox.Show("Esta seguro que desea eliminar este registro", "Elimina Registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End Sub
End Class